<html>
<head><meta charset="utf-8"><title>must_use but require a specific function is called? · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/must_use.20but.20require.20a.20specific.20function.20is.20called.3F.html">must_use but require a specific function is called?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="213683468"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/must_use%20but%20require%20a%20specific%20function%20is%20called%3F/near/213683468" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/must_use.20but.20require.20a.20specific.20function.20is.20called.3F.html#213683468">(Oct 18 2020 at 02:26)</a>:</h4>
<p>I have an API sort of similar to <code>File</code>:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">pub</span><span class="w"> </span><span class="k">struct</span> <span class="nc">S</span><span class="p">;</span><span class="w"></span>
<span class="k">impl</span><span class="w"> </span><span class="n">S</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">do_things</span><span class="p">(</span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="bp">self</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">unimplemented</span><span class="o">!</span><span class="p">();</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="sd">/// This function must be called; `drop()` panics.</span>
<span class="w">    </span><span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">end</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">unimplemented</span><span class="o">!</span><span class="p">();</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>

<p>Is there a way to get a compiler warning if <code>end()</code> isn't called? I tried <code>must_use</code>, but the warning goes away if you call <code>.do_things()</code> which isn't what I want.</p>



<a name="213683542"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/must_use%20but%20require%20a%20specific%20function%20is%20called%3F/near/213683542" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/must_use.20but.20require.20a.20specific.20function.20is.20called.3F.html#213683542">(Oct 18 2020 at 02:29)</a>:</h4>
<p>this is not quite the same as <a href="#narrow/stream/122651-general/topic/Opt-in.20guaranteed.20destructors">https://rust-lang.zulipchat.com/#narrow/stream/122651-general/topic/Opt-in.20guaranteed.20destructors</a> because I don't need it to be enforced by the compiler, I just want a lint</p>



<a name="213683546"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/must_use%20but%20require%20a%20specific%20function%20is%20called%3F/near/213683546" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/must_use.20but.20require.20a.20specific.20function.20is.20called.3F.html#213683546">(Oct 18 2020 at 02:29)</a>:</h4>
<p>(also this isn't Drop)</p>



<a name="213684716"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/must_use%20but%20require%20a%20specific%20function%20is%20called%3F/near/213684716" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/must_use.20but.20require.20a.20specific.20function.20is.20called.3F.html#213684716">(Oct 18 2020 at 03:05)</a>:</h4>
<p>If you can make all <code>S</code> methods take <code>self</code> and return <code>Self</code>, you can get <code>must_use</code> effects everywhere.</p>



<a name="213684769"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/must_use%20but%20require%20a%20specific%20function%20is%20called%3F/near/213684769" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/must_use.20but.20require.20a.20specific.20function.20is.20called.3F.html#213684769">(Oct 18 2020 at 03:06)</a>:</h4>
<p>oh that's clever</p>



<a name="213684776"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/must_use%20but%20require%20a%20specific%20function%20is%20called%3F/near/213684776" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/must_use.20but.20require.20a.20specific.20function.20is.20called.3F.html#213684776">(Oct 18 2020 at 03:07)</a>:</h4>
<p>this is not actually <em>my</em> API so I'm not sure if the owner wants that change, but clever</p>



<a name="213697568"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/must_use%20but%20require%20a%20specific%20function%20is%20called%3F/near/213697568" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> isHavvy <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/must_use.20but.20require.20a.20specific.20function.20is.20called.3F.html#213697568">(Oct 18 2020 at 09:19)</a>:</h4>
<p>Could be <code>#[does_not_use]</code></p>



<a name="213701878"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/must_use%20but%20require%20a%20specific%20function%20is%20called%3F/near/213701878" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/must_use.20but.20require.20a.20specific.20function.20is.20called.3F.html#213701878">(Oct 18 2020 at 11:14)</a>:</h4>
<p>How come <em>this isn't <code>Drop</code></em> and yet <em><code>drop()</code> panics</em>?</p>
<p>One option, but is rather to <em>force</em> the user into calling specific closing functions, is to use a callback pattern with a return type that proves the <code>end</code> method was called:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">pub</span><span class="w"></span>
<span class="k">struct</span> <span class="nc">S</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="o">..</span><span class="p">.</span><span class="w"> </span><span class="p">}</span><span class="w"></span>

<span class="k">pub</span><span class="w"></span>
<span class="k">struct</span> <span class="nc">CalledEnd</span><span class="p">(());</span><span class="w"></span>

<span class="k">impl</span><span class="w"> </span><span class="n">S</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">pub</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span><span class="c1">// private</span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">new</span><span class="w"> </span><span class="p">(</span><span class="o">..</span><span class="p">.)</span><span class="w"> </span>-&gt; <span class="nc">Self</span><span class="w"></span>
<span class="w">    </span><span class="p">{</span><span class="w"> </span><span class="o">..</span><span class="p">.</span><span class="w"> </span><span class="p">}</span><span class="w"></span>

<span class="w">    </span><span class="k">pub</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">with_new</span><span class="w"> </span><span class="p">(</span><span class="o">..</span><span class="p">.,</span><span class="w"> </span><span class="n">f</span>: <span class="nc">impl</span><span class="w"> </span><span class="nb">FnOnce</span><span class="p">(</span><span class="bp">Self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">CalledEnd</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">f</span><span class="p">(</span><span class="bp">Self</span>::<span class="n">new</span><span class="p">());</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>

<span class="w">    </span><span class="k">pub</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">end</span><span class="w"> </span><span class="p">(</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">CalledEnd</span><span class="w"></span>
<span class="w">    </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="c1">// ...;</span>
<span class="w">        </span><span class="n">CalledEnd</span><span class="p">(())</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>

<p>It is not <em>guaranteed</em> the right <code>end()</code> is called (unless you start using generative lifetimes), but as a "strong lint" this pattern can be quite powerful</p>



<a name="213704418"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/must_use%20but%20require%20a%20specific%20function%20is%20called%3F/near/213704418" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/must_use.20but.20require.20a.20specific.20function.20is.20called.3F.html#213704418">(Oct 18 2020 at 12:32)</a>:</h4>
<p>Yeah, I found your internals post from a while back <span aria-label="laughing" class="emoji emoji-1f606" role="img" title="laughing">:laughing:</span> it works but it's a little invasive.</p>
<blockquote>
<p>How come this isn't drop</p>
</blockquote>
<p>It returns an error that has to be handled.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>